/*
 * 
 * LegendShop 版权所有 2009-2011,并保留所有权利。
 * 
 * 官方网站：http://www.legendesign.net
 */
package com.legendshop.util;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.legendshop.oa.handler.impl.ContextServiceLocator;
import com.legendshop.oa.model.Menu;
import com.legendshop.permission.service.MenuManager;

/**
 *  后台菜单帮助类
 */
public class AdminMenuUtil {
	
	private static MenuManager menuManager =  (MenuManager) ContextServiceLocator.getInstance().getBean("menuManager");
	
	/**
	 * 计算当前菜单的情况
	 * @param request
	 */
	public static Menu parseMenu(HttpServletRequest request, Integer order){
		HttpSession session = request.getSession();
		Menu currentMenu = (Menu)session.getAttribute("currentMenu");
			if(currentMenu != null && currentMenu.getMenuId().intValue() == order){//菜单没有变化
				return currentMenu;
			}
		
		List<Menu> menuList = menuManager.getTopMenus(session);
		currentMenu =  menuList.isEmpty() ? null : menuList.get(0);
		if( order != 0 ){
			for (Menu menu : menuList) {
				if(menu.getMenuId().intValue() == order){
					currentMenu = menu;
					break;
				}
			}
		}
		session.setAttribute("currentMenu", currentMenu);
		session.setAttribute("currentMenuId", order);
		 return currentMenu;
	}

}
